package com.zy.mc_module.utils;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.widget.TextView;

import com.zy.mc_module.view.RoundBackgroundColorSpan;


public class TextUtils {

    public static String[] getTime(long time) {
        time /= 1000;
        long second = 0, minute = 0, hour = 0;
        if (time > 0) {
            second = time % 60;
            time /= 60;
        }
        if (time > 0) {
            minute = time % 60;
            time /= 60;
        }
        if (time > 0) {
            hour = time;
        }
        if (hour > 0) {
            return new String[]{String.valueOf(hour), String.valueOf(minute), String.valueOf(second),};
        }
        else {
            return new String[]{String.valueOf(minute), String.valueOf(second),};
        }
    }

    public static Spanned setTime(long time, String bgColor, String textColor, int padding) {
        time /= 1000;
        long second = 0, minute = 0, hour = 0;
        if (time > 0) {
            second = time % 60;
            time /= 60;
        }
        if (time > 0) {
            minute = time % 60;
            time /= 60;
        }
        if (time > 0) {
            hour = time;
        }

        String temp = "";
        String strHour, strMinute, strSecond;
        strHour = String.format("%02d", hour) + " : ";
        strMinute = String.format("%02d", minute) + " : ";
        strSecond = String.format("%02d", second);
        SpannableStringBuilder spannable = new SpannableStringBuilder(temp + strHour + strMinute + strSecond);
        int end1 = temp.length() + strHour.length() - 3;
        spannable.setSpan(new RelativeSizeSpan(0.9f), temp.length(), end1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannable.setSpan(new RoundBackgroundColorSpan(Color.parseColor(bgColor), Color.parseColor(textColor), padding, padding), temp.length(), end1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        int end2 = temp.length() + strHour.length() + strMinute.length() - 3;
        spannable.setSpan(new RelativeSizeSpan(0.9f), temp.length() + strHour.length(), end2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannable.setSpan(new RoundBackgroundColorSpan(Color.parseColor(bgColor), Color.parseColor(textColor), padding, padding), temp.length() + strHour.length(), end2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        int end = temp.length() + strHour.length() + strMinute.length() + strSecond.length();
        spannable.setSpan(new RelativeSizeSpan(0.9f), temp.length() + strHour.length() + strMinute.length(), end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannable.setSpan(new RoundBackgroundColorSpan(Color.parseColor(bgColor), Color.parseColor(textColor), padding, padding), temp.length() + strHour.length() + strMinute.length(), end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannable;
    }

    public static void setPriceDouble(TextView tv, String cont, float beforeSize, float endSize, String bgColor, String textColor, int padding) {
        String temp = "¥";
        SpannableStringBuilder spannable = new SpannableStringBuilder(temp + cont);
        spannable.setSpan(relativeSizeSpan(beforeSize), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (spannable.length() > 2)
            spannable.setSpan(relativeSizeSpan(endSize), spannable.length() - 2, spannable.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (bgColor != null && textColor != null)
            spannable.setSpan(roundBackgroundColorSpan(bgColor, textColor, padding), 0, spannable.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(spannable);
    }

    public static RoundBackgroundColorSpan roundBackgroundColorSpan(String bgColor, String textColor, int padding) {
        return new RoundBackgroundColorSpan(Color.parseColor(bgColor), Color.parseColor(textColor), padding, padding);
    }

    public static RelativeSizeSpan relativeSizeSpan(float proportion) {
        return new RelativeSizeSpan(proportion);
    }

    public static void clipText(Context context, String text) {
        ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("text", text);
        cmb.setPrimaryClip(clip);
        ToastUtil.show("复制成功");
    }
}
